R Lists Basics

We've covered Vectors, Matrices, and Data frames, now we will learn about our last basic built-in R data structure: the list.

Lists will allow us to store a variety of data structures under a single variable. This means we could store a vecor,matrix, data frame, etc. under a single list. For example:

In [19]:
# Create a vector
v <- c(1,2,3,4,5)

# Create a matrix
m <- matrix(1:10,nrow=2)

# Create a data frame
df <- women
In [20]:
v
Out[20]:
  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
In [21]:
m
Out[21]:
13579
2 4 6 810
In [22]:
df
Out[22]:
heightweight
158115
259117
360120
461123
562126
663129
764132
865135
966139
1067142
1168146
1269150
1370154
1471159
1572164

Using list()

We can use the list() to combine all the data structures:

In [23]:
li <- list(v,m,df)
In [24]:
li
Out[24]:
    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
  1. 13579
    2 4 6 810
  2. heightweight
    158115
    259117
    360120
    461123
    562126
    663129
    764132
    865135
    966139
    1067142
    1168146
    1269150
    1370154
    1471159
    1572164

You will notice that the list() assigned numbers to each of the objects in the list, but we can also assign names in the following manner:

In [25]:
li <- list(sample_vec = v,sample_mat = m, sample_df = df)
In [26]:
# Ignore the "error in vapply", this won't occur in RStudio!
li
Error in vapply(seq_along(mapped), function(i) {: values must be length 1,
 but FUN(X[[3]]) result is length 0
Out[26]:
$sample_vec
  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
$sample_mat
13579
2 4 6 810
$sample_df
heightweight
158115
259117
360120
461123
562126
663129
764132
865135
966139
1067142
1168146
1269150
1370154
1471159
1572164

Selecting objects from a list

You can use bracket notation to show objects in a list, and double brackets to actually grab the objects form the list, for example:

In [31]:
# Single brackets
li[1] # By index
Out[31]:
$sample_vec =
  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
In [34]:
# By name
li['sample_vec']
Out[34]:
$sample_vec =
  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
In [35]:
# Notice the type!
class(li['sample_vec'])
Out[35]:
'list'
In [36]:
# Use double brackets to actually grab the items
li[['sample_vec']]
Out[36]:
  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
In [38]:
# Can also use $ notation
li$sample_vec
Out[38]:
  1. 1
  2. 2
  3. 3
  4. 4
  5. 5

You can also index on top of this once you've selected the object from the list, for example:

In [39]:
li[['sample_vec']][1] # Second set of indexing
Out[39]:
1
In [40]:
li[['sample_mat']]
Out[40]:
13579
2 4 6 810
In [41]:
li[['sample_mat']][1,]
Out[41]:
  1. 1
  2. 3
  3. 5
  4. 7
  5. 9
In [42]:
li[['sample_mat']][1:2,1:2]
Out[42]:
13
24
In [43]:
li[['sample_df']]['height']
Out[43]:
height
158
259
360
461
562
663
764
865
966
1067
1168
1269
1370
1471
1572

Combining lists

Lists can hold other lists! You can also combine lists using the combine function c():

In [44]:
double_list <- c(li,li)
In [45]:
double_list
Error in vapply(seq_along(mapped), function(i) {: values must be length 1,
 but FUN(X[[3]]) result is length 0
Out[45]:
$sample_vec
  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
$sample_mat
13579
2 4 6 810
$sample_df
heightweight
158115
259117
360120
461123
562126
663129
764132
865135
966139
1067142
1168146
1269150
1370154
1471159
1572164
$sample_vec
  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
$sample_mat
13579
2 4 6 810
$sample_df
heightweight
158115
259117
360120
461123
562126
663129
764132
865135
966139
1067142
1168146
1269150
1370154
1471159
1572164
In [46]:
str(double_list)
List of 6
 $ sample_vec: num [1:5] 1 2 3 4 5
 $ sample_mat: int [1:2, 1:5] 1 2 3 4 5 6 7 8 9 10
 $ sample_df :'data.frame':	15 obs. of  2 variables:
  ..$ height: num [1:15] 58 59 60 61 62 63 64 65 66 67 ...
  ..$ weight: num [1:15] 115 117 120 123 126 129 132 135 139 142 ...
 $ sample_vec: num [1:5] 1 2 3 4 5
 $ sample_mat: int [1:2, 1:5] 1 2 3 4 5 6 7 8 9 10
 $ sample_df :'data.frame':	15 obs. of  2 variables:
  ..$ height: num [1:15] 58 59 60 61 62 63 64 65 66 67 ...
  ..$ weight: num [1:15] 115 117 120 123 126 129 132 135 139 142 ...

That's it for lists and that completes our section on basic data structures!